#!/bin/bash

# default to update source then build

set -x 

TOP_DIR=$(pwd)
SRC_DIR=$TOP_DIR/lib_source
BD_DIR=$TOP_DIR/lib_build
BIN_DIR=$TOP_DIR/lib_bin

VER_OPT=3.0.0

export PATH=$TOP_DIR/cross/bin:$PATH
# export LDFLAGS="-Wl,--export-all-symbols"
# export CFLAGS="-pipe -g"
# export CXXFLAGS="-pipe -g"

# export MAKE_OPT="-j 2"

cmd=all64
if [ -n "$1" ] ; then
	cmd=$1
fi

if [ "$cmd" == "all64" ] || [ "$cmd" == "all32" ] || [ "$cmd" == "update" ] ; then
	mkdir -p $SRC_DIR || exit 0
	cd $SRC_DIR
	if [ ! -d wxWidgets-$VER_OPT ] ; then
		rm -rf wxWidgets*
		wget -O wxWidgets-$VER_OPT.tar.bz2 http://sourceforge.net/projects/wxwindows/files/$VER_OPT/wxWidgets-$VER_OPT.tar.bz2/download
		tar xjf wxWidgets-$VER_OPT.tar.bz2
	fi
	cd ..
fi


if [ "$cmd" == "all64" ] || [ "$cmd" == "b64" ] || [ "$cmd" == "all32" ] || [ "$cmd" == "b32" ] ; then
	HOST_FLAG="--host=x86_64-w64-mingw32"
	if [ "$cmd" == "all32" ] || [ "$cmd" == "b32" ] ; then
		HOST_FLAG="--host=i686-w64-mingw32"
	fi
	rm -rf $BIN_DIR/wxWidgets
	mkdir -p $BD_DIR
	cd $BD_DIR
	rm -rf bd_wxWidgets
	mkdir bd_wxWidgets
	cd bd_wxWidgets
#	patch -d $SRC_DIR/wxWidgets-$VER_OPT -p0 < $TOP_DIR/patch/wxWidgets-2.9.patch
	# mingw-w64 r5480 add _vswprintf func, which not exist in xp x64, 
#	export CPPFLAGS="-D__USE_MINGW_ANSI_STDIO"
	$SRC_DIR/wxWidgets-$VER_OPT/configure $HOST_FLAG --prefix=$BIN_DIR/wxWidgets \
			--with-opengl --disable-compat26 --enable--compat28 \
			--enable-no_rtti --enable-no_deps \
			--disable-static --enable-shared

#	--disable-dependency-tracking \

#   --disable-precomp-headers
#			--enable-monolithic \
#	--disable-static --enable-shared \
#	--enable-debug --enable-optimise

	make $MAKE_OPT
	make install
	# ( cd $BIN_DIR/wxWidgets/include ; ln -s wx-2.8/wx wx ; cd $BD_DIR/bd_wxWidgets )
	mv $BIN_DIR/wxWidgets/lib/wx*.dll $BIN_DIR/wxWidgets/bin/
#	patch -d $SRC_DIR/wxWidgets-$VER_OPT -p0 -R < $TOP_DIR/patch/wxWidgets-2.9.patch
	cd ..
	cd ..
fi

